package cn.tannn.ops.util;

import cn.tannn.jdevelops.exception.built.BusinessException;
import cn.tannn.jdevelops.result.response.ResultVO;
import cn.tannn.ops.common.message.MessageSsl;
import cn.tannn.ops.message.entity.WebHook;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import lombok.SneakyThrows;
import org.jose4j.base64url.internal.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;

/**
 * 测试 webhook
 *
 * @author <a href="https://t.tannn.cn/">tan</a>
 * @version V1.0
 * @date 2024/8/23 下午2:26
 */
public class WebHookUtil {

    /**
     * 消息发送
     * @param webHook WebHook
     * @param message message
     * @return reboot返回的消息提示
     */
    public static ResultVO<String> sendMessage(WebHook webHook, MessageSsl message) {
        switch (webHook.getApp()){
            case DINGTALK -> {
                return dingtalkRobot(webHook,message);
            }
            case FEISHU -> {
                return feishuRobot(webHook,message);
            }
            case WECHAT -> {
                return wechatRobot(webHook,message);
            }
            default -> throw new BusinessException("暂不支持");
        }
    }

    /**
     * 企业微信发送消息
     * @param webHook webHook
     * @param message message
     */
    private static ResultVO<String> wechatRobot(WebHook webHook, MessageSsl message) {
        return ResultVO.failMessage("暂不支持企业微信");
    }

    /**
     * 飞书发送消息
     * @param webHook webHook
     * @param message message
     */
    private static ResultVO<String> feishuRobot(WebHook webHook, MessageSsl message) {
        return ResultVO.failMessage("暂不支持飞书");
    }

    /**
     * 钉钉发送消息
     * @param webHook webHook
     * @param message message
     */
    @SneakyThrows
    private static ResultVO<String> dingtalkRobot(WebHook webHook, MessageSsl message){
        Long timestamp = System.currentTimeMillis();
        String secret = webHook.getSecret();
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
        //sign字段和timestamp字段必须拼接到请求URL上，否则会出现 310000 的错误信息
        DingTalkClient client = new DefaultDingTalkClient(
                webHook.getUrl()+ "&sign="+sign+"&timestamp="+timestamp);
        OapiRobotSendRequest req = new OapiRobotSendRequest();
        /**
         * 发送文本消息
         */
        //定义文本内容
        OapiRobotSendRequest.Markdown text = new OapiRobotSendRequest.Markdown();
        text.setTitle(message.getTitle());
        text.setText(message.toString());
//        //定义 @ 对象
//        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
//        at.setAtUserIds(Arrays.asList(USER_ID));
        //设置消息类型 https://open.dingtalk.com/document/orgapp/custom-bot-send-message-type?spm=ding_open_doc.document.0.0.ff327f7f4OTfz0
        req.setMsgtype("markdown");
        req.setMarkdown(text);
//        req.setAt(at);
        OapiRobotSendResponse rsp = client.execute(req);
        if(rsp.isSuccess()){
            return ResultVO.successMessage(rsp.getMessage());
        }else {
            return ResultVO.failMessage(rsp.getErrmsg());
        }
    }



}
